﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using tju.cme.BookStore2024.webapi.Models.Tools;
using tju.cme.BookStore2024.webapi.Models.ViewModels.ReaderNovel;
using tju.cme.BookStore2024.webapi.Services;

namespace tju.cme.BookStore2024.webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ReaderNovelController(ReaderNovelServices services): ControllerBase
    {
        /// <summary>
        /// 创建阅读记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<IActionResult> Greate(ReadHistorylGreateModel model)
        {
            await services.ReadHistoryGreateAsync(model, GetAuthorizedInfo.GetId(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 收藏或取消收藏小说
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        [Authorize(Roles = "Reader")]
        [HttpPost("IsCollect/{novelId}")]
        public async Task<IActionResult> IsCollect([FromRoute]Guid novelId)
        {
            await services.IsCollectNovelAsync(GetAuthorizedInfo.GetId(User),novelId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 根据读者ID获取阅读记录
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            return Ok(new ReturnMessage(await services.GetAllRecordAsync(GetAuthorizedInfo.GetId(User))));
        }
    }
}
